import {routerRedux} from 'dva/router';
import modelExtend from 'dva-model-extend';
import {message} from "antd";
import pathToRegexp from 'path-to-regexp';
import queryString from 'query-string';
import {pageModel} from './base';
import {login, logout, create, getPage, get, update, remove, removeByIds} from '../services/customer';
import {setLocalStorage, clearLocalStorage} from '../utils/storagehelper';
import {arrayRemove} from '../utils/utils';

export default modelExtend(pageModel, {
  namespace: 'customer',

  state: {
    selectedRowKeys: [],
  },

  subscriptions: {
    setup({dispatch, history}) {
      return history.listen(({pathname, search}) => {
        if (pathToRegexp('/customers').exec(pathname)) {
          const payload = queryString.parse(search) || {
            page: 1,
            per_page: 10
          };
          dispatch({
            type: 'getPage',
            payload,
          });
        }
      });
    },
  },

  effects: {
    * login({payload}, {call, put}) {
      const result = yield call(login, payload);
      if (result && result.code === 0) {
        // result.data.customer.menus = '1, 2, 3, 4, 5, 6, 10, 201, 202, 1001, 1002, 20201, 20202, 20203, 100101, 100102, 100103, 1202, 1301, 1302, 1303, 1304, 1305, 1306';
        setLocalStorage('current_customer', result.data);
        yield put(routerRedux.push('/'));
      }
    },

    * logout({payload}, {call, put}) {
      yield call(logout, payload);
      clearLocalStorage();
      yield put(routerRedux.push('/customers_login'));
    },

    * create({payload}, {call, put}) {
      const result = yield call(create, payload);
      if (result && result.code === 0) {
        message.success(result.message);
        yield put(routerRedux.goBack());
      }
    },

    * get({payload}, {call, put}) {
      const result = yield call(get, payload);
      if (result && result.code === 0) {
        yield put({
          type: 'bSaveItem',
          payload: result.data,
        });
      }
    },

    * update({payload}, {call, put}) {
      const result = yield call(update, payload);
      if (result && result.code === 0) {
        message.success(result.message);
        yield put(routerRedux.goBack());
      }
    },

    * remove({payload}, {call, put}) {
      const result = yield call(remove, payload);
      if (result && result.code === 0) {
        message.success(result.message);
        yield put({
          type: 'removeSelectedKeys',
          payload: payload,
        });
        yield put({
          type: 'reload',
        });
      }
    },

    * removeByIds(_, {call, put, select}) {
      const {selectedRowKeys} = yield select(state => state.customer);
      const result = yield call(removeByIds, {ids: selectedRowKeys});
      if (result && result.code === 0) {
        message.success(result.message);
        yield put({
          type: 'cleanSelectedKeys',
        });
        yield put({
          type: 'reload',
        });
      }
    },

    * reload(_, {put, select}) {
      const {params} = yield select(state => state.customer);
      yield put({
        type: 'getPage',
        payload: params,
      });
    },

    * getPage({payload}, {put}) {
      yield put({
        type: 'bGetPage',
        payload: {
          getPage,
          params: payload,
        },
      });
    },
  },

  reducers: {
    cleanSelectedKeys(state) {
      return {
        ...state,
        selectedRowKeys: [],
      };
    },

    removeSelectedKeys(state, {payload}) {
      arrayRemove(state.selectedRowKeys, payload);
      return {
        ...state,
        selectedRowKeys: state.selectedRowKeys,
      };
    },

    cleanItem(state) {
      return {
        ...state,
        item: {},
      };
    },
  },
});
